/*
* Copyright (c) 2007 by Kirill Kolodyazhniy.
* See the file "license.terms" for information on usage and redistribution.
*/

#include "rendersystem.h"
#include "rendertarget.h"
#include "renderdata.h"
#include "functors.h"

#include <algorithm>

namespace Engine
{
	using namespace std;
	/**************************************************************************************************************/
	RenderSystem::RenderSystem()
		:m_facesCount(0)
	{
	}
	/**************************************************************************************************************/
	RenderSystem::~RenderSystem()
	{
		ShutDown();
	}
	/**************************************************************************************************************/
	void RenderSystem::ShutDown()
	{
		m_renderTargets.clear();
		m_prioritisedRenderTargets.clear();
	}
	/**************************************************************************************************************/
	void RenderSystem::AttachRenderTarget(RefPtr<RenderTarget,WeakPtrExtension> renderTarget)
	{
		m_renderTargets.insert( RenderTargetMap::value_type( renderTarget->GetName(), renderTarget ) );
        m_prioritisedRenderTargets.insert( RenderTargetPriorityMap::value_type(renderTarget->GetPriority(), renderTarget ));
	}
	/**************************************************************************************************************/
	void RenderSystem::UpdateAllRenderTargets()
	{
		for_each(m_prioritisedRenderTargets.begin(), m_prioritisedRenderTargets.end(), on2nd(m_fn(&RenderTarget::Update)));
	}
	/**************************************************************************************************************/
	void RenderSystem::Render(RenderDataPtr renderData)
	{
		m_facesCount += renderData->GetPrimitiveCount();	
	}
	/**************************************************************************************************************/
}
